package com.android;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Submit extends Activity implements OnClickListener {
	//valuex = time, valueXx = turns
	public int valTurns = 0;
	public int valTime = 0;
	
	//create a database object
	Database database = new Database(this);
	SQLiteDatabase db;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.submit);
        //add a button
        Button submit = (Button) findViewById(R.id.button1);
        submit.setOnClickListener(this);
        //get the 2 values sent from the GameBoard class
        Bundle b = getIntent().getExtras();

        valTurns = b.getInt("value", 0);
        valTime = b.getInt("valuex", 0);
    }

	@Override
	public void onClick(View v) {
		EditText tv=(EditText)findViewById(R.id.editText1);
		if(v == findViewById(R.id.button1))
		{
			//if(tv.getText() != null){
				//**************TRYING TEXTFILES AS STORAGE**********************
				//try {
					  // open myfilename.txt for writing
					  //OutputStreamWriter out = new OutputStreamWriter(openFileOutput("highscore.txt",0));
					  // write the contents on mySettings to the file
					  //EditText tv=(EditText)findViewById(R.id.editText1);
					 // out.append(tv.getText() + " - Turns: " + value + "\n");
					  //out.append(tv.getText() + " - Turns: " + value + "\n");
					 // out.append(tv.getText() + " - Turns: " + value + "\n");
					  // close the file
					  //out.flush();
					 // out.close();
					//} catch (java.io.IOException e) {
					  //do something if an IOException occurs.
					//	TextView t1=(TextView)findViewById(R.id.error);
					//	t1.setText("Turns: error");
					//}
				//****************************************************************
				//writeable database
				db = database.getWritableDatabase();
				//this is the player name field
				
				//time to store the data
				ContentValues value = new ContentValues();
				//adding name, time and turns to the Content
				value.put(Database.ColName, tv.getText().toString());
				value.put(Database.ColTime, valTime);
				value.put(Database.ColTurns, valTurns);
				//do it! inserting the data to the table.
				db.insertWithOnConflict(Database.TABLEname, null, value, SQLiteDatabase.CONFLICT_REPLACE);
				//close the database
				db.close();
				//close and start a new activity
				this.finish();
	    		Intent intent  = new Intent(getBaseContext(), HighScore.class);
	    		startActivity(intent);
			//}
		}
		
	}
}
